﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Payroll.BusinessLogic.Package;
using Payroll.BusinessLogic.Data;
using Payroll.Common.Utils;
using Payroll.Common;
using Payroll.BusinessLogic.Caching;

namespace Payroll.BusinessLogic.Package
{
    public partial class PackageService : IPackageService
    {
        private readonly PayrollHRDataContext m_context;
        private readonly ICacheManager _cacheManager;
        public PackageService(PayrollHRDataContext context)
        {
            this.m_context = context;
            this._cacheManager = new PayrollRequestCache();
        }
        public List<PackagePrice> GetAllPackagePrice(PackageSearch filter, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            DateTime dateApply = filter.PriceDate.HasValue ? filter.PriceDate.Value : DateTime.Now;
            var query = from p in m_context.PackagePriceGetDynamicContents(dateApply.ToString("yyyy-MM-dd"), filter.PackName, filter.PackType, filter.PackDate)
                        select new PackagePrice
                        {
                            PackagePricesID = p.PackagePricesID,
                            PackCode = p.PackCode,
                            Name = p.Name,
                            Price = p.Price,
                            PackTime = p.PackageTypeName,
                            PackTypeName = p.PackageTypeName,
                            DateApply = p.PriceDate
                        };
            return query.ToList();
            
        }
        public int SavePackagePrice(List<PackagePrice> List)
        {
            foreach (var package in List)
            {
                var packageColection = this.GetPackageByCode(package.PackCode);
                if(!CommonHelper.IsNull(packageColection))
                {
                    this.SavePackagePrice(new Data.PackagePrice { PackageID = packageColection.PackagesID, PriceDate = CommonHelper.ToSQLDate(package.DateApply), Price = package.Price });
                }
            }

            return 0;
        }
        protected int SavePackagePrice(Data.PackagePrice p)
        { 
            var item = (from pc in m_context.PackagePrices
                       where pc.PackageID == p.PackageID && pc.PriceDate == p.PriceDate
                       select pc).FirstOrDefault();
            if (!CommonHelper.IsNull(item))
            {
                item.Price = p.Price;
            }
            else {
                m_context.PackagePrices.InsertOnSubmit(p);
                m_context.SubmitChanges();
            }
            return 0;
        }

        public int Save(Package p)
        {
            var item = (from pk in m_context.Packages
                        where pk.PackagesID == p.PackagesID && pk.PackActive == true
                        select pk).FirstOrDefault();
            p.Name = p.Name.ToUpper();
            if (CommonHelper.IsNull(item))
            {
                item = new BusinessLogic.Data.Package
                {

                    #region Assign
                    Name = p.Name,
                    PackCode = p.PackCode,
                    PackDate = p.PackDate,
                    PackActive = p.PackActive,
                    PackType = p.PackType,
                    Createby = p.Createby,
                    Modified = p.Modified,
                    CreateDate = p.CreateDate,
                    Description1 = p.Description1,
                    Description2 = p.Description2,
                    Description3 = p.Description3,
                    ModifiedDate = p.ModifiedDate

                    #endregion
                };
                m_context.Packages.InsertOnSubmit(item);
            }
            else
            {
                #region CustomerInfo
                item.Name = p.Name;
                item.PackCode = p.PackCode;
                item.PackDate = p.PackDate;
                item.PackActive = p.PackActive;
                item.PackType = p.PackType;
                item.Createby = p.Createby;
                item.Modified = p.Modified;
                item.CreateDate = p.CreateDate;
                item.ModifiedDate = p.ModifiedDate;
                item.Description1 = p.Description1;
                item.Description2 = p.Description2;
                item.Description3 = p.Description3;
                #endregion
            }
            m_context.SubmitChanges();

            return item.PackagesID;
        }
        public Package GetPackageById(int Packid)
        {
            var query = from p in m_context.Packages
                        where p.PackagesID == Packid //&& o2.ObjectTypeId == 1
                        select new Package
                        {
                            #region
                            PackagesID = p.PackagesID,
                            Name = p.Name,
                            PackCode = p.PackCode,
                            PackDate = p.PackDate,
                            PackActive = p.PackActive,
                            PackType = p.PackType,
                            Createby = p.Createby,
                            Modified = p.Modified,
                            CreateDate = p.CreateDate,
                            Description1 = p.Description1,
                            Description2 = p.Description2,
                            Description3 = p.Description3,
                            ModifiedDate = p.ModifiedDate
                            #endregion
                        };
            return query.FirstOrDefault();
        }
        public Package GetPackageByName(string name = "")
        {
            var query = from p in m_context.Packages
                        where p.PackActive == true && p.Name.ToUpper() == name.ToUpper()
                        select new Package
                        {
                            #region
                            PackagesID = p.PackagesID,
                            Name = p.Name,
                            PackCode = p.PackCode,
                            PackDate = p.PackDate,
                            PackActive = p.PackActive,
                            PackType = p.PackType,
                            Createby = p.Createby,
                            Modified = p.Modified,
                            CreateDate = p.CreateDate,
                            Description1 = p.Description1,
                            Description2 = p.Description2,
                            Description3 = p.Description3,
                            ModifiedDate = p.ModifiedDate
                            #endregion
                        };
            return query.FirstOrDefault();
        }
        public Package GetPackageByCode(string code = "")
        {
            var query = from p in m_context.Packages
                        where p.PackActive == true && p.PackCode == code
                        select new Package
                        {
                            #region
                            PackagesID = p.PackagesID,
                            Name = p.Name,
                            PackCode = p.PackCode,
                            PackDate = p.PackDate,
                            PackActive = p.PackActive,
                            PackType = p.PackType,
                            Createby = p.Createby,
                            Modified = p.Modified,
                            CreateDate = p.CreateDate,
                            Description1 = p.Description1,
                            Description2 = p.Description2,
                            Description3 = p.Description3,
                            ModifiedDate = p.ModifiedDate
                            #endregion
                        };
            return query.FirstOrDefault();
        }
        public PagedList<Package> GetAllPackage(PackageSearch filter, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            var query = from p in m_context.Packages
                        join pt in m_context.PackageTimes on p.PackDate equals pt.PackageTimeId into PackageTimeJoin
                        from pt2 in PackageTimeJoin.DefaultIfEmpty()
                        join pt3 in m_context.PackageTypes on p.PackType equals pt3.PackageTypeId into PackageTypeJoin
                        from pt4 in PackageTypeJoin.DefaultIfEmpty()
                        join e in m_context.Employees on p.Createby equals e.EmployeeId into employeeJoin
                        from e2 in employeeJoin.DefaultIfEmpty()
                        where p.PackActive == true
                        && (p.PackDate == filter.PackDate || filter.PackDate == 0 || filter.PackDate == null)
                        && (filter.PackName.Contains(p.Name) || filter.PackName == "" || filter.PackName == null)
                        && (p.PackType == filter.PackType || filter.PackType == 0 || filter.PackType == null)
                        orderby p.PackDate ascending
                        select new Package
                        {
                            #region
                            PackagesID = p.PackagesID,
                            Name = p.Name,
                            PackCode = p.PackCode,
                            PackDate = p.PackDate,
                            PackActive = p.PackActive,
                            PackType = p.PackType,
                            Createby = p.Createby,
                            Modified = p.Modified,
                            CreateDate = p.CreateDate,
                            Description1 = p.Description1,
                            Description2 = p.Description2,
                            Description3 = p.Description3,
                            ModifiedDate = p.ModifiedDate,
                            PackTime = pt2.PackageTimeName,
                            PackTypeName = pt4.PackageTypeName,
                            CreatebyName = e2.Name
                            #endregion
                        };
            return new PagedList<Package>(query, pageIndex, pageSize);
        }
        public int Delete(List<int> List)
        {

            foreach (var item in List)
            {
                var pack = (from p in m_context.Packages
                            where p.PackagesID == item
                            select p).FirstOrDefault();
                if (!CommonHelper.IsNull(item))
                {
                    pack.PackActive = false;
                    m_context.SubmitChanges();
                }
            }
            return 0;
        }


        public void Delete(string packagePricesID)
        {
            List<int> List = packagePricesID.Split(',').Select(Int32.Parse).ToList();
            var query = from p in m_context.PackagePrices
                        where (from id in List select id).Contains(p.PackagePricesID)
                        select p;
            m_context.PackagePrices.DeleteAllOnSubmit(query);
            m_context.SubmitChanges();
        }


        public List<Package> GetAllPackage()
        {
            var query = from p in m_context.Packages
                        join pt in m_context.PackageTimes on p.PackDate equals pt.PackageTimeId into PackageTimeJoin
                        from pt2 in PackageTimeJoin.DefaultIfEmpty()
                        join pt3 in m_context.PackageTypes on p.PackType equals pt3.PackageTypeId into PackageTypeJoin
                        from pt4 in PackageTypeJoin.DefaultIfEmpty()
                        where p.PackActive == true
                        select new Package { PackagesID = p.PackagesID, Name = p.Name, PackCode = p.PackCode };
            return query.ToList();
        }
    }
}
